package com.rw.controller;

import com.mysql.cj.x.protobuf.MysqlxDatatypes;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.PageContext;
import java.io.IOException;

@Controller
public class TestController {


    @RequestMapping("req")
    public String getIndex(String name,HttpServletRequest request){
        request.setAttribute("msg","request设置的参数");
        request.setAttribute("name",name);
        System.out.println("getIndex方法执行");
        return "req.jsp";
    }



    @GetMapping("session1")
    public String getSessions1(String name,HttpServletRequest request){
        request.getSession().setAttribute("msg","request获取的session设置的参数");
        request.setAttribute("name",name);
        System.out.println("TestController.getSessions1");
        return "sess.jsp";
    }

    @RequestMapping("session2")
    public String getSessions2(String name, HttpSession session){
        session.setAttribute("msg","session设置的参数");
        session.setAttribute("name",name);
        System.out.println("TestController.getSessions2");
        return "sess.jsp";
    }

    @RequestMapping("app")
    public String getApplication(String name, HttpServletRequest request){
        ServletContext servletContext = request.getServletContext();
        servletContext.setAttribute("msg","ServletContext设置的参数");
        servletContext.setAttribute("name",name);
        System.out.println("TestController.getApplication");
        return "sess.jsp";
    }

    @RequestMapping("getModel")
    public String getModel(String name, Model model){
        model.addAttribute("msg","来至model设置的数据");
        model.addAttribute("name",name);
        return "req.jsp";
    }


    @RequestMapping("req1")
    public String getRe1(String name,Integer age,HttpServletRequest request){
        request.setAttribute("msg","request第一次设置的参数");
        System.out.println("getRe1方法执行"+name+":"+age);
        return "req2";
    }

    @RequestMapping("req2")
    public String getRe2(String name,HttpServletRequest request){
        request.setAttribute("msg","request第二次设置的参数"+name+":");

        request.setAttribute("name",name);
       // request.setAttribute("age",age);
        System.out.println("getRe2方法执行"+name+":");
        return "req.jsp";
    }


    @RequestMapping("redict")
    public String getRedict(String name,Integer age,Model model){
//        request.setAttribute("msg","request第一次设置的参数");
        System.out.println("getRe1方法执行"+name+":"+age);
        model.addAttribute("a",name);
        model.addAttribute("b",age);
        return "redirect:/redict2";
    }

    @RequestMapping("redict2")
    public String getRedict2(String name,Integer age,HttpServletRequest request){
//        request.setAttribute("msg","request第二次设置的参数"+name+

//        request.setAttribute("name",name);
        // request.setAttribute("age",age);
        System.out.println("getRe2方法执行"+name+":"+age);
        return "req.jsp";
    }

    @RequestMapping("resp")
    public void getResp(String name, Integer age, HttpServletResponse response) throws IOException {
//        request.setAttribute("msg","request第二次设置的参数"+name+":");

//        request.setAttribute("name",name);
        // request.setAttribute("age",age);
        System.out.println("getResp方法执行"+name+":"+age);
        String url = "redirect:/sess.jsp?name="+name+"&age="+age;
        System.out.println(url);
        response.sendRedirect(url);
    }


    @RequestMapping("t3")
    public String test3(){

        System.out.println("TestController.test3----访问了xxx.jsp");
        return "test";

    }

    @RequestMapping("{url}")
    public String tt(@PathVariable String url){
        System.out.println("resetful风格访问");
        return url;
    }}
